from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import CarRank, UserProfile

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)
    
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']
    
    def __init__(self, *args, **kwargs):
        super(UserRegisterForm, self).__init__(*args, **kwargs)
        # 添加Bootstrap样式
        for field_name in self.fields:
            self.fields[field_name].widget.attrs.update({'class': 'form-control'})

class UserLoginForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '用户名'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': '密码'}))

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['phone', 'avatar']
        widgets = {
            'phone': forms.TextInput(attrs={'class': 'form-control'}),
            'avatar': forms.FileInput(attrs={'class': 'form-control'}),
        }

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ['username', 'email']
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
        }

class CarRankForm(forms.ModelForm):
    class Meta:
        model = CarRank
        fields = '__all__'
        widgets = {
            'series_id': forms.NumberInput(attrs={'class': 'form-control'}),
            'series_name': forms.TextInput(attrs={'class': 'form-control'}),
            'image': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'ranks': forms.NumberInput(attrs={'class': 'form-control'}),
            'min_price': forms.NumberInput(attrs={'class': 'form-control'}),
            'max_price': forms.NumberInput(attrs={'class': 'form-control'}),
            'last_rank': forms.NumberInput(attrs={'class': 'form-control'}),
            'count': forms.NumberInput(attrs={'class': 'form-control'}),
            'score': forms.NumberInput(attrs={'class': 'form-control'}),
            'car_review_count': forms.NumberInput(attrs={'class': 'form-control'}),
            'show_trend': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
            'descender_price': forms.NumberInput(attrs={'class': 'form-control'}),
            'series_pic_count': forms.NumberInput(attrs={'class': 'form-control'}),
            'brand_id': forms.NumberInput(attrs={'class': 'form-control'}),
            'brand_name': forms.TextInput(attrs={'class': 'form-control'}),
            'sub_brand_id': forms.NumberInput(attrs={'class': 'form-control'}),
            'sub_brand_name': forms.TextInput(attrs={'class': 'form-control'}),
            'price': forms.TextInput(attrs={'class': 'form-control'}),
            'dealer_price': forms.TextInput(attrs={'class': 'form-control'}),
            'has_dealer_price': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
        } 